home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / speedt.arc / SPEED.C next >
Text File  |  1986-09-27  |  6KB  |  294 lines

  1.  
  2. /*
  3.  * cpuspeed - general system performance benchmark
  4.  *
  5.  * S.H.Smith, 26-Sep-86
  6.  *
  7.  */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <dos.h>
  12. #include <assert.h>
  13.  
  14. void   dispspeed();
  15. void   memspeed();
  16. void   cpuspeed();
  17. void   mathspeed();
  18. void   filespeed();
  19. void   bufspeed();
  20. long   get_time();
  21. char   *malloc();
  22. FILE   *output;
  23. long   start;
  24. long   stop;
  25. double elapsed;
  26. double index;
  27. double total;
  28. int    count;
  29. int    pass;
  30. int    iterations = 1;
  31.  
  32. #define round(d) ((d) + 0.004995)
  33.  
  34. main(argc,argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.    if (argc == 2)
  39.       iterations = atoi(argv[1]);
  40.    else
  41.  
  42.    if (argc != 1) {
  43.       printf("Usage: SPEED           ;1 iteration\n");
  44.       printf(" or    SPEED N         ;N iterations\n");
  45.       exit(1);
  46.    }
  47.  
  48.    count = 0;
  49.    total = 0;
  50.    output = fopen( "CON","w" );
  51.    scrollscreen();
  52.  
  53.    bench( dispspeed, "BIOS display updates" );
  54.    bench( memspeed,  "Heavy memory access" );
  55.    bench( cpuspeed,  "Minimal memory access" );
  56.    bench( mathspeed, "Floating point" );
  57.    bench( bufspeed,  "Buffered file access" );
  58.    bench( filespeed, "General file access" );
  59.  
  60.    total /= (double)count;
  61.    index = (iterations*10) / total;
  62.  
  63.    fprintf( output,"%28s  --------       --------\n"," ");
  64.    fprintf( output,"  %25s %8.1f       %8.2f\n\n",
  65.                    "Average performance",round(total),round(index) );
  66.  
  67.    fflush( output );
  68.    fclose( output );
  69. }
  70.  
  71.  
  72. bench( procedure, description )
  73. void (*procedure)();
  74. char *description;
  75. {
  76.    fprintf( output, "   %-25s",description );
  77.    fflush( output );
  78.  
  79.    (*procedure)();
  80.  
  81.    elapsed = (double)(stop-start) / 100.0;
  82.    total += elapsed;
  83.    count++;
  84.  
  85.    if (elapsed == 0.0)
  86.       index = 9999.0;
  87.    else
  88.       index = (iterations*10) / elapsed;
  89.  
  90.    if (count == 1) {
  91.       scrollscreen();
  92.       fprintf( output,"\n                System performance benchmark\n\n" );
  93.       fprintf( output,"All tests run in %4.1f seconds on a standard 8088 IBM PC with\n",10.0*iterations);
  94.       fprintf( output,"4.77 MHz clock, 640k ram, 360k floppy disk, no math processor.\n\n");
  95.       fprintf( output,"           Test                 Time     Performance index\n" );
  96.       fprintf( output,"----------------------------  --------  -------------------\n");
  97.       fprintf( output, "   %-25s",description );
  98.    }
  99.  
  100.    fprintf( output,"%8.1f       %8.2f \n",round(elapsed),round(index) );
  101.    fflush( output );
  102. }
  103.  
  104.  
  105. /*
  106.  * benchmark procedures
  107.  *
  108.  * each procedure should run for exactly 10.0 seconds per call on
  109.  * a standard IBM-PC.
  110.  *
  111.  */
  112.  
  113. void dispspeed()   /* BIOS display updates */
  114. {
  115.    const int DISPSIZE = 5328;
  116.    char *buf,*p;
  117.    int fd;
  118.    int i;
  119.  
  120.    assert( (p = buf = malloc(DISPSIZE)) != 0 );
  121.  
  122.    for (i=0; i<DISPSIZE; i++)
  123.       *p++ = (i & 0x3f) + ' ';
  124.  
  125.    for (i=1000; i<1050; i++)
  126.       buf[i] = '\n';
  127.  
  128.    for (i=2000; i<2050; i++)
  129.       buf[i] = '\t';
  130.  
  131.    start = get_time();
  132.  
  133.    fd = open( "con",1 );
  134.    for (pass=0; pass<iterations; pass++)
  135.       write( fd,buf,DISPSIZE );
  136.    close( fd );
  137.  
  138.    stop = get_time();
  139.    free( buf );
  140. }
  141.  
  142.  
  143. void memspeed()   /* heavy memory access bench */
  144. {
  145.    const int LOOPS = 100;
  146.    const int MEMSIZ = 26350;
  147.    char *buf1,*buf2;
  148.    int i;
  149.  
  150.    assert( (buf1 = malloc( MEMSIZ )) != 0 );
  151.    assert( (buf2 = malloc( MEMSIZ )) != 0 );
  152.  
  153.    start = get_time();
  154.  
  155.    for (pass=0; pass<iterations; pass++)
  156.       for (i=0; i<LOOPS; i++)
  157.          memcpy( buf2,buf1,MEMSIZ );
  158.  
  159.    stop = get_time();
  160.    free( buf1 );
  161.    free( buf2 );
  162. }
  163.  
  164.  
  165. void cpuspeed()   /* minimal memory access */
  166. {
  167.    const int LOOPS = 100;
  168.    register int i,j;
  169.  
  170.    start = get_time();
  171.  
  172.    for (pass=0; pass<iterations; pass++)
  173.       for (i=0; i<LOOPS; i++)
  174.          for (j=0; j<4956; j++);
  175.  
  176.    stop = get_time();
  177. }
  178.  
  179.  
  180. void mathspeed()   /* floating point */
  181. {
  182.    const int LOOPS = 770;
  183.    double a,b,c;
  184.    int i;
  185.  
  186.    a = 123.45;
  187.    b = 111.12;
  188.    c = 1.7;
  189.    start = get_time();
  190.  
  191.    for (pass=0; pass<iterations; pass++)
  192.       for (i=0; i<LOOPS; i++)
  193.          c = c + (a*b) + (a/b) - (b/a)*c;
  194.  
  195.    stop = get_time();
  196. }
  197.  
  198.  
  199. void bufspeed()   /* (large) buffered file access */
  200. {
  201.    const int LOOPS = 12;
  202.    const int IOBSIZE = 4096;
  203.    int fd,i;
  204.    char *buf;
  205.    char *name = "$$$$$$$$.$$$";
  206.  
  207.    assert( (buf = malloc( IOBSIZE )) != 0 );
  208.  
  209.    newfile( name,IOBSIZE,LOOPS );
  210.    start = get_time();
  211.  
  212.    for (pass=0; pass<iterations; pass++) {
  213.       assert( (fd = open( name,2 )) > 0);
  214.       for (i=0; i<LOOPS; i++)
  215.          write( fd,buf,IOBSIZE );
  216.       close( fd );
  217.  
  218.       assert( (fd = open( name,2 )) > 0);
  219.       for (i=0; i<LOOPS; i++)
  220.          read( fd,buf,IOBSIZE );
  221.       close( fd );
  222.    }
  223.  
  224.    stop = get_time();
  225.    unlink( name );
  226.    free( buf );
  227. }
  228.  
  229.  
  230. void filespeed()   /* general (small buffer) file access */
  231. {
  232.    const int LOOPS = 349; /*262*/
  233.    int fd,i;
  234.    char buf[30];
  235.    char *name = "$$$$$$$$.$$$";
  236.  
  237.    newfile( name,sizeof(buf),LOOPS );
  238.    start = get_time();
  239.  
  240.    for (pass=0; pass<iterations; pass++) {
  241.       assert( (fd = open( name,2 )) > 0);
  242.       for (i=0; i<LOOPS; i++)
  243.          write( fd,buf,sizeof(buf) );
  244.       close( fd );
  245.  
  246.       assert( (fd = open( name,2 )) > 0);
  247.       for (i=0; i<LOOPS; i++)
  248.          read( fd,buf,sizeof(buf) );
  249.       close( fd );
  250.    }
  251.  
  252.    stop = get_time();
  253.    unlink( name );
  254. }
  255.  
  256.  
  257.  
  258. /* scroll cursor to erase screen and leave cursor at the LAST line */
  259. scrollscreen()
  260. {
  261.    fprintf( output,"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
  262.    fflush( output );
  263. }
  264.  
  265.  
  266.  
  267. /* gets the time since midnight in seconds*100 */
  268. long get_time()
  269. {
  270.    union REGS in, out;
  271.    #define hi(vv) ((out.x.vv>>8) & 0xff)
  272.    #define lo(vv) (out.x.vv & 0xff)
  273.  
  274.    in.x.ax = 0x2c00;          /* DOS get time function code */
  275.    intdos( &in, &out );
  276.  
  277.    return lo(dx) + 100L*(hi(dx) + 60L*(lo(cx) + 24L*(hi(cx))));
  278. }
  279.  
  280.  
  281.  
  282. /* create a new (empty) file with a given initial size */
  283. newfile(name,elements,size)
  284. char *name;
  285. unsigned elements,size;
  286. {
  287.    int fd;
  288.    unsigned size;
  289.  
  290.    assert( (fd = creat(name,0)) > 0);
  291.    write( fd,&fd,elements*size );
  292.    close( fd );
  293. }
  294.